package aod.android.com.bernay.live.view;

import android.content.Context;
import android.graphics.drawable.ColorDrawable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.PopupWindow;
import android.widget.TextView;

import aod.android.com.bernay.R;

/**
 * <p>文件描述：照片视频选择 <p>
 * <p>作者：${SYC}<p>
 * <p>创建时间：2019/4/16<p>
 */
public class PhotoPopupWindow extends PopupWindow implements View.OnClickListener{
    private Context mContext;
    private View view;

    private TextView picture_tv_photo = null;
    private TextView picture_tv_video = null;
    private TextView picture_tv_cancel = null;

    private String selectType = "";

    public PhotoPopupWindow(Context context, String selectType) {
        super(context);
        mContext = context;
        this.selectType = selectType;
        init();
    }

    private void init() {
        /**
         * 注意：我们的接口同时作为成员变量传入，因为我们用于监听子Item的数据监听
         * */
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.picture_camera_pop_layout, null);

        initView();
        this.setContentView(view);
        //自定义基础，设置我们显示控件的宽，高，焦点，点击外部关闭PopupWindow操作
        this.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
        this.setFocusable(true);
        this.setOutsideTouchable(true);
//        //更新试图
//        this.update();
        //设置背景
        ColorDrawable colorDrawable = new ColorDrawable(0000000000);
        setBackgroundDrawable(colorDrawable);
    }

    private void initView() {
        picture_tv_photo = view.findViewById(R.id.picture_tv_photo);
        picture_tv_video = view.findViewById(R.id.picture_tv_video);
        picture_tv_cancel = view.findViewById(R.id.picture_tv_cancel);

        if (TextUtils.isEmpty(selectType)){
            picture_tv_photo.setVisibility(View.VISIBLE);
            picture_tv_video.setVisibility(View.VISIBLE);
        }else if (selectType.equals("0")){//选择图片
            picture_tv_photo.setVisibility(View.VISIBLE);
            picture_tv_video.setVisibility(View.GONE);
        }else if (selectType.equals("1")){//选择视频
            picture_tv_photo.setVisibility(View.GONE);
            picture_tv_video.setVisibility(View.VISIBLE);
        }

        picture_tv_photo.setOnClickListener(this);
        picture_tv_video.setOnClickListener(this);
        picture_tv_cancel.setOnClickListener(this);
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(PhotoPopupWindow.OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.picture_tv_photo:
                onItemClickListener.onItemPhotoClick(0);
                break;
            case R.id.picture_tv_video:
                onItemClickListener.onItemVideoClick(1);
                break;
            case R.id.picture_tv_cancel:
                dismiss();
                break;
        }
    }

    public interface OnItemClickListener {
        void onItemPhotoClick(int positon);
        void onItemVideoClick(int positon);
    }
}
